﻿// 
// Copyright (c) 2013 Jason Bell
// 
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// 

using System;
using System.Collections.Generic;

namespace Gharen.GLCore.X11
{
    public class X11Platform
        : Platform
    {
        List<DisplayMode> displayModes = new List<DisplayMode>();

        public X11Platform()
        {
            EnumerateDisplayModes();
        }

        public override IntPtr CopyMemory(IntPtr destination, IntPtr source, int numBytes)
        {
            return X.memcpy(destination, source, numBytes);
        }

        public override System.Collections.ObjectModel.ReadOnlyCollection<DisplayMode> DisplayModes
        {
            get { return this.displayModes.AsReadOnly(); }
        }

        void EnumerateDisplayModes()
        {
            IntPtr display = X.OpenDisplay(IntPtr.Zero);
            if (display == IntPtr.Zero)
                throw new Exception("Could not connect to the X server.");

            int screen = X.DefaultScreen(display);

            int version = 0;
            if (X.QueryExtension(display, "RANDR", out version, out version, out version) == 0)
                throw new Exception("RANDR extension not available.");

            IntPtr config = X.XRRGetScreenInfo(display, X.RootWindow(display, screen));
            if (config == IntPtr.Zero)
                throw new Exception("Error getting screen info.");

            int nbSizes;

            IntPtr size = X.XRRConfigSizes(config, out nbSizes);
            if (nbSizes == 0)
                throw new Exception("Error getting config sizes.");
		
			
			unsafe
			{
				
				XRRScreenSize * sizesPtr = (XRRScreenSize *)size.ToPointer();	
				
				for(int i = 0; i < nbSizes; i++)
					this.displayModes.Add (new DisplayMode(sizesPtr[i].Width,
					                                       sizesPtr[i].Height,
					                                       60));
				
			}

            int [] nbDepths = X.ListDepths(display, screen);
            for (int i = 0; i < nbDepths.Length; i++)
                Console.WriteLine(nbDepths[i]);
			
			
        }
    }
}
